package com.xuecheng.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanMediaDataVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.web.exceptions.BusinessException;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.content.service.TeachplanMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 七安
 */
@Slf4j
@RestController
@RequestMapping("/teachplanMedia")
public class TeachplanMediaController {

    @Autowired
    private TeachplanMediaService teachplanMediaService;

    @SneakyThrows
    @GetMapping("/{mediaId}")
    public ResponseResult<TeachplanMediaDataVo> findTmByMediaId(@PathVariable Long mediaId) {
        //判空
        if (mediaId == null) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getMediaId, mediaId);
        wrapper.last("limit 1");
        //查询
        TeachplanMedia teachplanMedia = teachplanMediaService.getOne(wrapper);
        //判空
        if (ObjectUtils.isEmpty(teachplanMedia)) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        TeachplanMediaDataVo teachplanMediaDataVo = BeanHelper.copyProperties(teachplanMedia, TeachplanMediaDataVo.class);
        return ResponseResult.okResult(teachplanMediaDataVo);
    }
}
